; ===================================================================
; _HTTPPost_files($host, $page, [$socket], [$data], [$filenames])
;
; Executes a POST request on an open socket.
; Parameters:
;   $host - IN - The hostname you want to get the page from. This should be in the format "www.google.com" or "localhost"
;   $page - IN - The the file you want to get. This should always start with a slash. Examples: "/" or "/somedirectory/submitform.php"
;   $socket - OPTIONAL IN - The socket opened by _HTTPConnect. If this is not supplied, the last socket opened with _HTTPConnect will be used.
;   $data - array of data to send in the post request. This should first be run through _HTTPEncodeString()
;   $filenames - array of paths for files to send
; Returns:
;   The number of bytes sent in the request.
; Author: Val Polyakh <scriptguru@gmail.com>
; Requires: Base64.au3
; Remarks:
;   Possible @errors:
;   1 - No socket supplied and no current socket exists
;   2 - Error sending to socket. Check @extended for Windows API WSAGetError return
; ===================================================================
Func _HTTPPost_files($host, $page, $socket, $data, $filenames)
    
    Local $b="---------------------------0123456789012"
    Local $fh,$image,$str,$picdata,$fieldname, $arr, $header
    Local $command = ""
    
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
;$command &= $data&@CRLF
    
    For $i=0 To (UBound($data)-1)
        $arr=StringSplit($data[$i],"=",2)
        $command &= "--"& $b &@CRLF &"Content-Disposition: form-data; name="& $arr[0] & @CRLF&@CRLF & $arr[1] &@CRLF
        
    Next
    
    For $i=0 To (UBound($filenames)-1)
        $arr=StringSplit($filenames[$i],"\",2);get filename
        $fieldname=$arr[UBound($arr)-1]
        $arr=StringSplit($fieldname,".",2);chop extension
        $fieldname=$arr[0]
        $fh=FileOpen($filenames[$i], 16)
        $image=FileRead($fh)
        FileClose($fh)
        $str=_Base64Encode($image)
        $command &="--"& $b _
            &@CRLF&"Content-Disposition: form-data; name=" & $fieldname &"; filename="& $filenames[$i] _
            &@CRLF&"Content-Type: application/upload" _
            &@CRLF&"Content-Transfer-Encoding: base64"&@CRLF&@CRLF _
            &$str &@CRLF
    Next

    $command &= @CRLF&"--"& $b &"--"&@CRLF

    Dim $datasize = StringLen($command)
    $header = "POST "&$page&" HTTP/1.1"&@CRLF
    $header &= "Host: " &$host&@CRLF
    $header &= "User-Agent: "&$_HTTPUserAgent&@CRLF
	$header &= "GE-computerName: "&@ComputerName&@CRLF
	$header &= "GE-computerHash: "&$computerHash&@CRLF
    $header &= "Connection: close"&@CRLF
    $header &= "Content-Type: multipart/form-data; boundary=" & $b & @CRLF
    $header &= "Content-Length: "&$datasize&@CRLF
    $header &= ""&@CRLF

    $command= $header & $command
    Dim $bytessent = TCPSend($socket, $command)
    
    If $bytessent == 0 Then
        SetExtended(@error)
        SetError(2)
        return 0
    EndIf
    
    SetError(0)
    Return $bytessent
    
EndFunc